package com.ordertracker; /**
 * Description of class.
 * Created 11/23/12 7:29 PM
 * @author andrey.rodin@playtech.com  
 */

import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.WebApplicationContext;

import javax.servlet.ServletContextEvent;

public class ApplicationLifeCycleListener extends ContextLoaderListener  {

    private ApplicationLifeCycle lifeCycle = new DefaultLifeCycle();
    private ApplicationConfig config;

    // Public constructor is required by servlet spec
    public ApplicationLifeCycleListener() {
        super();
        config = new ApplicationConfig();
        this.lifeCycle.preStart(config);
        System.out.println("ApplicationLifeCycleListener: constructor");
    }

    public ApplicationLifeCycleListener(WebApplicationContext context) {
        super(context);
        System.out.println("ApplicationLifeCycleListener: constructor2");

    }

    @Override
    public void contextInitialized(ServletContextEvent event) {
        super.contextInitialized(event);
        config.setApplicationContext(getCurrentWebApplicationContext());
        lifeCycle.postStart(config);
        System.out.println("ApplicationLifeCycleListener: contextInitialized");
    }

    @Override
    public void contextDestroyed(ServletContextEvent event) {
        lifeCycle.preStop(config);
        super.contextDestroyed(event);
    }
}
